Skip to content

Conversation

@Casperento
Copy link
Contributor

This modification will enable the usage of MergeFunctions as a standalone library. Currently, MergeFunctions can only be applied to an entire module. By adopting this change, developers will gain the flexibility to reuse the MergeFunctions code within their own projects, choosing which functions to merge; hence, promoting code reusability. Notice that this modification will not break backward compatibility, because MergeFunctions will still work as a pass after the modification.

Summary of Changes:

  • Modified the MergeFunctionsPass to allow running the pass over a set of function pointers.
  • This behavior is optional and doesn't interfere with the existing functionality of running the pass on the entire Module.
  • Added unit tests to assert the correctness of the updated implementation, ensuring that function merging works as expected when run on both sets of pointers and full modules.

remove templates

unit tests added

format

updated data structures

format
@github-actions
Copy link

github-actions bot commented Oct 3, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Oct 3, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Rafael Eckstein (Casperento)

Changes

This modification will enable the usage of MergeFunctions as a standalone library. Currently, MergeFunctions can only be applied to an entire module. By adopting this change, developers will gain the flexibility to reuse the MergeFunctions code within their own projects, choosing which functions to merge; hence, promoting code reusability. Notice that this modification will not break backward compatibility, because MergeFunctions will still work as a pass after the modification.

Summary of Changes:

  • Modified the MergeFunctionsPass to allow running the pass over a set of function pointers.
  • This behavior is optional and doesn't interfere with the existing functionality of running the pass on the entire Module.
  • Added unit tests to assert the correctness of the updated implementation, ensuring that function merging works as expected when run on both sets of pointers and full modules.

Full diff: https://github.com/llvm/llvm-project/pull/111045.diff

5 Files Affected:

  • (modified) llvm/include/llvm/Transforms/IPO/MergeFunctions.h (+7)
  • (modified) llvm/lib/Transforms/IPO/MergeFunctions.cpp (+61-2)
  • (modified) llvm/unittests/Transforms/Utils/CMakeLists.txt (+1)
  • (added) llvm/unittests/Transforms/Utils/MergeFunctionsTest.cpp (+271)
  • (modified) llvm/utils/gn/secondary/llvm/unittests/Transforms/Utils/BUILD.gn (+1)
diff --git a/llvm/include/llvm/Transforms/IPO/MergeFunctions.h b/llvm/include/llvm/Transforms/IPO/MergeFunctions.h
index 822f0fd99188d0..1b3b1d22f11e28 100644
--- a/llvm/include/llvm/Transforms/IPO/MergeFunctions.h
+++ b/llvm/include/llvm/Transforms/IPO/MergeFunctions.h
@@ -15,7 +15,10 @@
 #ifndef LLVM_TRANSFORMS_IPO_MERGEFUNCTIONS_H
 #define LLVM_TRANSFORMS_IPO_MERGEFUNCTIONS_H
 
+#include "llvm/IR/Function.h"
 #include "llvm/IR/PassManager.h"
+#include <map>
+#include <set>
 
 namespace llvm {
 
@@ -25,6 +28,10 @@ class Module;
 class MergeFunctionsPass : public PassInfoMixin<MergeFunctionsPass> {
 public:
   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
+
+  static bool runOnModule(Module &M);
+  static std::pair<bool, std::map<Function *, Function *>>
+  runOnFunctions(std::set<Function *> &F);
 };
 
 } // end namespace llvm
diff --git a/llvm/lib/Transforms/IPO/MergeFunctions.cpp b/llvm/lib/Transforms/IPO/MergeFunctions.cpp
index b50a700e09038f..a434d7920b6ccf 100644
--- a/llvm/lib/Transforms/IPO/MergeFunctions.cpp
+++ b/llvm/lib/Transforms/IPO/MergeFunctions.cpp
@@ -123,6 +123,7 @@
 #include <algorithm>
 #include <cassert>
 #include <iterator>
+#include <map>
 #include <set>
 #include <utility>
 #include <vector>
@@ -198,6 +199,8 @@ class MergeFunctions {
   }
 
   bool runOnModule(Module &M);
+  bool runOnFunctions(std::set<Function *> &F);
+  std::map<Function *, Function *> &getDelToNewMap();
 
 private:
   // The function comparison operator is provided here so that FunctionNodes do
@@ -298,17 +301,31 @@ class MergeFunctions {
   // dangling iterators into FnTree. The invariant that preserves this is that
   // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree.
   DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree;
+
+  /// Deleted-New functions mapping
+  std::map<Function *, Function *> DelToNewMap;
 };
 } // end anonymous namespace
 
 PreservedAnalyses MergeFunctionsPass::run(Module &M,
                                           ModuleAnalysisManager &AM) {
-  MergeFunctions MF;
-  if (!MF.runOnModule(M))
+  if (!MergeFunctionsPass::runOnModule(M))
     return PreservedAnalyses::all();
   return PreservedAnalyses::none();
 }
 
+bool MergeFunctionsPass::runOnModule(Module &M) {
+  MergeFunctions MF;
+  return MF.runOnModule(M);
+}
+
+std::pair<bool, std::map<Function *, Function *>>
+MergeFunctionsPass::runOnFunctions(std::set<Function *> &F) {
+  MergeFunctions MF;
+  bool MergeResult = MF.runOnFunctions(F);
+  return {MergeResult, MF.getDelToNewMap()};
+}
+
 #ifndef NDEBUG
 bool MergeFunctions::doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist) {
   if (const unsigned Max = NumFunctionsForVerificationCheck) {
@@ -468,6 +485,47 @@ bool MergeFunctions::runOnModule(Module &M) {
   return Changed;
 }
 
+bool MergeFunctions::runOnFunctions(std::set<Function *> &F) {
+  bool Changed = false;
+  std::vector<std::pair<IRHash, Function *>> HashedFuncs;
+  for (Function *Func : F) {
+    if (isEligibleForMerging(*Func)) {
+      HashedFuncs.push_back({StructuralHash(*Func), Func});
+    }
+  }
+  llvm::stable_sort(HashedFuncs, less_first());
+  auto S = HashedFuncs.begin();
+  for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) {
+    if ((I != S && std::prev(I)->first == I->first) ||
+        (std::next(I) != IE && std::next(I)->first == I->first)) {
+      Deferred.push_back(WeakTrackingVH(I->second));
+    }
+  }
+  do {
+    std::vector<WeakTrackingVH> Worklist;
+    Deferred.swap(Worklist);
+    LLVM_DEBUG(dbgs() << "size of function: " << F.size() << '\n');
+    LLVM_DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n');
+    for (WeakTrackingVH &I : Worklist) {
+      if (!I)
+        continue;
+      Function *F = cast<Function>(I);
+      if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) {
+        Changed |= insert(F);
+      }
+    }
+    LLVM_DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n');
+  } while (!Deferred.empty());
+  FnTree.clear();
+  FNodesInTree.clear();
+  GlobalNumbers.clear();
+  return Changed;
+}
+
+std::map<Function *, Function *> &MergeFunctions::getDelToNewMap() {
+  return this->DelToNewMap;
+}
+
 // Replace direct callers of Old with New.
 void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) {
   for (Use &U : llvm::make_early_inc_range(Old->uses())) {
@@ -1004,6 +1062,7 @@ bool MergeFunctions::insert(Function *NewFunction) {
 
   Function *DeleteF = NewFunction;
   mergeTwoFunctions(OldF.getFunc(), DeleteF);
+  this->DelToNewMap.emplace(DeleteF, OldF.getFunc());
   return true;
 }
 
diff --git a/llvm/unittests/Transforms/Utils/CMakeLists.txt b/llvm/unittests/Transforms/Utils/CMakeLists.txt
index 5c7ec28709c169..7effa5d8e7d6d2 100644
--- a/llvm/unittests/Transforms/Utils/CMakeLists.txt
+++ b/llvm/unittests/Transforms/Utils/CMakeLists.txt
@@ -26,6 +26,7 @@ add_llvm_unittest(UtilsTests
   LoopUtilsTest.cpp
   MemTransferLowering.cpp
   ModuleUtilsTest.cpp
+  MergeFunctionsTest.cpp
   ScalarEvolutionExpanderTest.cpp
   SizeOptsTest.cpp
   SSAUpdaterBulkTest.cpp
diff --git a/llvm/unittests/Transforms/Utils/MergeFunctionsTest.cpp b/llvm/unittests/Transforms/Utils/MergeFunctionsTest.cpp
new file mode 100644
index 00000000000000..696c5391ef4f68
--- /dev/null
+++ b/llvm/unittests/Transforms/Utils/MergeFunctionsTest.cpp
@@ -0,0 +1,271 @@
+//===- MergeFunctionsTest.cpp - Unit tests for
+//MergeFunctionsPass-----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/IPO/MergeFunctions.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/SourceMgr.h"
+#include "gtest/gtest.h"
+#include <memory>
+
+using namespace llvm;
+
+namespace {
+
+TEST(MergeFunctions, TrueOutputModuleTest) {
+  LLVMContext Ctx;
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M(parseAssemblyString(R"invalid(
+        @.str = private unnamed_addr constant [10 x i8] c"On f: %d\0A\00", align 1
+        @.str.1 = private unnamed_addr constant [13 x i8] c"On main: %d\0A\00", align 1
+
+        define dso_local i32 @f(i32 noundef %arg) #0 {
+            entry:
+                %add109 = call i32 @_slice_add10(i32 %arg)
+                %call = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %add109)
+                ret i32 %add109
+        }
+
+        declare i32 @printf(ptr noundef, ...) #1
+
+        define dso_local i32 @main(i32 noundef %argc, ptr noundef %argv) #0 {
+            entry:
+                %add99 = call i32 @_slice_add10(i32 %argc)
+                %call = call i32 @f(i32 noundef 2)
+                %sub = sub nsw i32 %call, 6
+                %call10 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %add99)
+                ret i32 %add99
+        }
+
+        define internal i32 @_slice_add10(i32 %arg) #2 {
+            sliceclone_entry:
+                %0 = mul nsw i32 %arg, %arg
+                %1 = mul nsw i32 %0, 2
+                %2 = mul nsw i32 %1, 2
+                %3 = mul nsw i32 %2, 2
+                %4 = add nsw i32 %3, 2
+                ret i32 %4
+        }
+
+        define internal i32 @_slice_add10_alt(i32 %arg) #2 {
+            sliceclone_entry:
+                %0 = mul nsw i32 %arg, %arg
+                %1 = mul nsw i32 %0, 2
+                %2 = mul nsw i32 %1, 2
+                %3 = mul nsw i32 %2, 2
+                %4 = add nsw i32 %3, 2
+                ret i32 %4
+        }
+
+        attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+        attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+        attributes #2 = { nounwind willreturn }
+    )invalid",
+                                                Err, Ctx));
+
+  // Expects true after merging _slice_add10 and _slice_add10_alt
+  EXPECT_TRUE(MergeFunctionsPass::runOnModule(*M));
+}
+
+TEST(MergeFunctions, TrueOutputFunctionsTest) {
+  LLVMContext Ctx;
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M(parseAssemblyString(R"invalid(
+        @.str = private unnamed_addr constant [10 x i8] c"On f: %d\0A\00", align 1
+        @.str.1 = private unnamed_addr constant [13 x i8] c"On main: %d\0A\00", align 1
+
+        define dso_local i32 @f(i32 noundef %arg) #0 {
+            entry:
+                %add109 = call i32 @_slice_add10(i32 %arg)
+                %call = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %add109)
+                ret i32 %add109
+        }
+
+        declare i32 @printf(ptr noundef, ...) #1
+
+        define dso_local i32 @main(i32 noundef %argc, ptr noundef %argv) #0 {
+            entry:
+                %add99 = call i32 @_slice_add10(i32 %argc)
+                %call = call i32 @f(i32 noundef 2)
+                %sub = sub nsw i32 %call, 6
+                %call10 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %add99)
+                ret i32 %add99
+        }
+
+        define internal i32 @_slice_add10(i32 %arg) #2 {
+            sliceclone_entry:
+                %0 = mul nsw i32 %arg, %arg
+                %1 = mul nsw i32 %0, 2
+                %2 = mul nsw i32 %1, 2
+                %3 = mul nsw i32 %2, 2
+                %4 = add nsw i32 %3, 2
+                ret i32 %4
+        }
+
+        define internal i32 @_slice_add10_alt(i32 %arg) #2 {
+            sliceclone_entry:
+                %0 = mul nsw i32 %arg, %arg
+                %1 = mul nsw i32 %0, 2
+                %2 = mul nsw i32 %1, 2
+                %3 = mul nsw i32 %2, 2
+                %4 = add nsw i32 %3, 2
+                ret i32 %4
+        }
+
+        attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+        attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+        attributes #2 = { nounwind willreturn }
+    )invalid",
+                                                Err, Ctx));
+
+  std::set<Function *> FunctionsSet;
+  for (Function &F : *M)
+    FunctionsSet.insert(&F);
+
+  std::pair<bool, std::map<Function *, Function *>> MergeResult =
+      MergeFunctionsPass::runOnFunctions(FunctionsSet);
+
+  // Expects true after merging _slice_add10 and _slice_add10_alt
+  EXPECT_TRUE(MergeResult.first);
+
+  // Expects that both functions (_slice_add10 and _slice_add10_alt)
+  // be mapped to the same new function
+  EXPECT_TRUE(MergeResult.second.size() > 0);
+  std::map<Function *, Function *> DelToNew = MergeResult.second;
+  Function *NewFunction = M->getFunction("_slice_add10");
+  for (auto P : DelToNew)
+    if (P.second)
+      EXPECT_EQ(P.second, NewFunction);
+}
+
+TEST(MergeFunctions, FalseOutputModuleTest) {
+  LLVMContext Ctx;
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M(parseAssemblyString(R"invalid(
+        @.str = private unnamed_addr constant [10 x i8] c"On f: %d\0A\00", align 1
+        @.str.1 = private unnamed_addr constant [13 x i8] c"On main: %d\0A\00", align 1
+
+        define dso_local i32 @f(i32 noundef %arg) #0 {
+            entry:
+                %add109 = call i32 @_slice_add10(i32 %arg)
+                %call = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %add109)
+                ret i32 %add109
+        }
+
+        declare i32 @printf(ptr noundef, ...) #1
+
+        define dso_local i32 @main(i32 noundef %argc, ptr noundef %argv) #0 {
+            entry:
+                %add99 = call i32 @_slice_add10(i32 %argc)
+                %call = call i32 @f(i32 noundef 2)
+                %sub = sub nsw i32 %call, 6
+                %call10 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %add99)
+                ret i32 %add99
+        }
+
+        define internal i32 @_slice_add10(i32 %arg) #2 {
+            sliceclone_entry:
+                %0 = mul nsw i32 %arg, %arg
+                %1 = mul nsw i32 %0, 2
+                %2 = mul nsw i32 %1, 2
+                %3 = mul nsw i32 %2, 2
+                %4 = add nsw i32 %3, 2
+                ret i32 %4
+        }
+
+        define internal i32 @_slice_add10_alt(i32 %arg) #2 {
+            sliceclone_entry:
+                %0 = mul nsw i32 %arg, %arg
+                %1 = mul nsw i32 %0, 2
+                %2 = mul nsw i32 %1, 2
+                %3 = mul nsw i32 %2, 2
+                %4 = add nsw i32 %3, 2
+                ret i32 %0
+        }
+
+        attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+        attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+        attributes #2 = { nounwind willreturn }
+    )invalid",
+                                                Err, Ctx));
+
+  // Expects false after trying to merge _slice_add10 and _slice_add10_alt
+  EXPECT_FALSE(MergeFunctionsPass::runOnModule(*M));
+}
+
+TEST(MergeFunctions, FalseOutputFunctionsTest) {
+  LLVMContext Ctx;
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M(parseAssemblyString(R"invalid(
+        @.str = private unnamed_addr constant [10 x i8] c"On f: %d\0A\00", align 1
+        @.str.1 = private unnamed_addr constant [13 x i8] c"On main: %d\0A\00", align 1
+
+        define dso_local i32 @f(i32 noundef %arg) #0 {
+            entry:
+                %add109 = call i32 @_slice_add10(i32 %arg)
+                %call = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %add109)
+                ret i32 %add109
+        }
+
+        declare i32 @printf(ptr noundef, ...) #1
+
+        define dso_local i32 @main(i32 noundef %argc, ptr noundef %argv) #0 {
+            entry:
+                %add99 = call i32 @_slice_add10(i32 %argc)
+                %call = call i32 @f(i32 noundef 2)
+                %sub = sub nsw i32 %call, 6
+                %call10 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, i32 noundef %add99)
+                ret i32 %add99
+        }
+
+        define internal i32 @_slice_add10(i32 %arg) #2 {
+            sliceclone_entry:
+                %0 = mul nsw i32 %arg, %arg
+                %1 = mul nsw i32 %0, 2
+                %2 = mul nsw i32 %1, 2
+                %3 = mul nsw i32 %2, 2
+                %4 = add nsw i32 %3, 2
+                ret i32 %4
+        }
+
+        define internal i32 @_slice_add10_alt(i32 %arg) #2 {
+            sliceclone_entry:
+                %0 = mul nsw i32 %arg, %arg
+                %1 = mul nsw i32 %0, 2
+                %2 = mul nsw i32 %1, 2
+                %3 = mul nsw i32 %2, 2
+                %4 = add nsw i32 %3, 2
+                ret i32 %0
+        }
+
+        attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+        attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
+        attributes #2 = { nounwind willreturn }
+    )invalid",
+                                                Err, Ctx));
+
+  std::set<Function *> FunctionsSet;
+  for (Function &F : *M)
+    FunctionsSet.insert(&F);
+
+  std::pair<bool, std::map<Function *, Function *>> MergeResult =
+      MergeFunctionsPass::runOnFunctions(FunctionsSet);
+
+  for (auto P : MergeResult.second)
+    std::cout << P.first << " " << P.second << "\n";
+
+  // Expects false after trying to merge _slice_add10 and _slice_add10_alt
+  EXPECT_FALSE(MergeResult.first);
+
+  // Expects empty map
+  EXPECT_EQ(MergeResult.second.size(), 0u);
+}
+
+} // namespace
\ No newline at end of file
diff --git a/llvm/utils/gn/secondary/llvm/unittests/Transforms/Utils/BUILD.gn b/llvm/utils/gn/secondary/llvm/unittests/Transforms/Utils/BUILD.gn
index 380ed71a2bc010..fcea55c91f083c 100644
--- a/llvm/utils/gn/secondary/llvm/unittests/Transforms/Utils/BUILD.gn
+++ b/llvm/utils/gn/secondary/llvm/unittests/Transforms/Utils/BUILD.gn
@@ -27,6 +27,7 @@ unittest("UtilsTests") {
     "LoopUtilsTest.cpp",
     "MemTransferLowering.cpp",
     "ModuleUtilsTest.cpp",
+    "MergeFunctionsTest.cpp",
     "ProfDataUtilTest.cpp",
     "SSAUpdaterBulkTest.cpp",
     "ScalarEvolutionExpanderTest.cpp",

@Casperento
Copy link
Contributor Author

Ping

@github-actions
Copy link

github-actions bot commented Nov 1, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@Casperento
Copy link
Contributor Author

Requesting for reviewers

@oskarwirga @SLTozer

Copy link
Contributor

@SLTozer SLTozer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more comments - structurally this looks fine, just have some code suggestions/questions.

@SLTozer
Copy link
Contributor

SLTozer commented Nov 22, 2024

As-of latest changes this LGTM, I think this shouldn't impact existing code either since it's just adding a new way to invoke MergeFunctions - just pinging @nikic in case they either have opinions about this change or know someone who might (I don't see any maintainers that cover this file or recent contributors to this area), otherwise I can test and merge it later.

MergeFunctionsPass::runOnFunctions(FunctionsSet.getArrayRef());

for (auto P : MergeResult)
std::cout << P.first << " " << P.second << "\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not noticing this before, but it looks like there's some leftover debugging code here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in 8703263.

@nikic nikic merged commit 2a6e589 into llvm:main Nov 28, 2024
7 checks passed
@github-actions
Copy link

@Casperento Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 28, 2024

LLVM Buildbot has detected a new failure on builder llvm-nvptx-nvidia-ubuntu running on as-builder-7 while building llvm at step 6 "test-build-unified-tree-check-llvm".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/180/builds/9213

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-llvm) failure: test (failure)
...
0.952 [2/9/680] Linking CXX executable unittests/ExecutionEngine/Orc/OrcJITTests
1.009 [2/8/681] Linking CXX executable unittests/tools/llvm-exegesis/LLVMExegesisTests
1.030 [2/7/682] Linking CXX executable unittests/CodeGen/CodeGenTests
1.036 [2/6/683] Linking CXX executable unittests/Frontend/LLVMFrontendTests
1.088 [2/5/684] Linking CXX executable unittests/Analysis/AnalysisTests
1.234 [2/4/685] Linking CXX executable unittests/IR/IRTests
1.277 [2/3/686] Linking CXX executable unittests/Support/SupportTests
1.308 [2/2/687] Linking CXX executable unittests/ADT/ADTTests
4.729 [2/1/688] Building CXX object unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o
4.898 [1/1/689] Linking CXX executable unittests/Transforms/Utils/UtilsTests
FAILED: unittests/Transforms/Utils/UtilsTests 
: && /usr/bin/c++ -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -fuse-ld=gold     -Wl,--gc-sections unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ASanStackFrameLayoutTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/BasicBlockUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CallPromotionUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CloningTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CodeExtractorTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CodeLayoutTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CodeMoverUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/DebugifyTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/FunctionComparatorTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/IntegerDivisionTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/LocalTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/LoopRotationUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/LoopUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MemTransferLowering.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ModuleUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ScalarEvolutionExpanderTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/SizeOptsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/SSAUpdaterBulkTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/UnrollLoopTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ValueMapperTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ProfDataUtilTest.cpp.o -o unittests/Transforms/Utils/UtilsTests  -Wl,-rpath,/home/buildbot/worker/as-builder-7/ramdisk/llvm-nvptx-nvidia-ubuntu/build/lib  lib/libLLVMPasses.so.20.0git  lib/libllvm_gtest_main.so.20.0git  lib/libllvm_gtest.so.20.0git  lib/libLLVMBitWriter.so.20.0git  lib/libLLVMVectorize.so.20.0git  lib/libLLVMTransformUtils.so.20.0git  lib/libLLVMAnalysis.so.20.0git  lib/libLLVMProfileData.so.20.0git  lib/libLLVMAsmParser.so.20.0git  lib/libLLVMCore.so.20.0git  lib/libLLVMSupport.so.20.0git  -Wl,-rpath-link,/home/buildbot/worker/as-builder-7/ramdisk/llvm-nvptx-nvidia-ubuntu/build/lib && :
unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o:MergeFunctionsTest.cpp:function (anonymous namespace)::MergeFunctions_TrueOutputModuleTest_Test::TestBody(): error: undefined reference to 'llvm::MergeFunctionsPass::runOnModule(llvm::Module&)'
unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o:MergeFunctionsTest.cpp:function (anonymous namespace)::MergeFunctions_FalseOutputModuleTest_Test::TestBody(): error: undefined reference to 'llvm::MergeFunctionsPass::runOnModule(llvm::Module&)'
unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o:MergeFunctionsTest.cpp:function (anonymous namespace)::MergeFunctions_TrueOutputFunctionsTest_Test::TestBody(): error: undefined reference to 'llvm::MergeFunctionsPass::runOnFunctions(llvm::ArrayRef<llvm::Function*>)'
unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o:MergeFunctionsTest.cpp:function (anonymous namespace)::MergeFunctions_FalseOutputFunctionsTest_Test::TestBody(): error: undefined reference to 'llvm::MergeFunctionsPass::runOnFunctions(llvm::ArrayRef<llvm::Function*>)'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 28, 2024

LLVM Buildbot has detected a new failure on builder llvm-nvptx64-nvidia-ubuntu running on as-builder-7 while building llvm at step 6 "test-build-unified-tree-check-llvm".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/160/builds/9215

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-llvm) failure: test (failure)
...
0.983 [2/9/680] Linking CXX executable unittests/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizerTests
1.017 [2/8/681] Linking CXX executable unittests/tools/llvm-exegesis/LLVMExegesisTests
1.026 [2/7/682] Linking CXX executable unittests/CodeGen/CodeGenTests
1.042 [2/6/683] Linking CXX executable unittests/Frontend/LLVMFrontendTests
1.099 [2/5/684] Linking CXX executable unittests/Analysis/AnalysisTests
1.205 [2/4/685] Linking CXX executable unittests/IR/IRTests
1.298 [2/3/686] Linking CXX executable unittests/Support/SupportTests
1.367 [2/2/687] Linking CXX executable unittests/ADT/ADTTests
4.496 [2/1/688] Building CXX object unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o
4.662 [1/1/689] Linking CXX executable unittests/Transforms/Utils/UtilsTests
FAILED: unittests/Transforms/Utils/UtilsTests 
: && /usr/bin/c++ -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -fuse-ld=gold     -Wl,--gc-sections unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ASanStackFrameLayoutTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/BasicBlockUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CallPromotionUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CloningTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CodeExtractorTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CodeLayoutTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/CodeMoverUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/DebugifyTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/FunctionComparatorTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/IntegerDivisionTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/LocalTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/LoopRotationUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/LoopUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MemTransferLowering.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ModuleUtilsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ScalarEvolutionExpanderTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/SizeOptsTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/SSAUpdaterBulkTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/UnrollLoopTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ValueMapperTest.cpp.o unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/ProfDataUtilTest.cpp.o -o unittests/Transforms/Utils/UtilsTests  -Wl,-rpath,/home/buildbot/worker/as-builder-7/ramdisk/llvm-nvptx64-nvidia-ubuntu/build/lib  lib/libLLVMPasses.so.20.0git  lib/libllvm_gtest_main.so.20.0git  lib/libllvm_gtest.so.20.0git  lib/libLLVMBitWriter.so.20.0git  lib/libLLVMVectorize.so.20.0git  lib/libLLVMTransformUtils.so.20.0git  lib/libLLVMAnalysis.so.20.0git  lib/libLLVMProfileData.so.20.0git  lib/libLLVMAsmParser.so.20.0git  lib/libLLVMCore.so.20.0git  lib/libLLVMSupport.so.20.0git  -Wl,-rpath-link,/home/buildbot/worker/as-builder-7/ramdisk/llvm-nvptx64-nvidia-ubuntu/build/lib && :
unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o:MergeFunctionsTest.cpp:function (anonymous namespace)::MergeFunctions_TrueOutputModuleTest_Test::TestBody(): error: undefined reference to 'llvm::MergeFunctionsPass::runOnModule(llvm::Module&)'
unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o:MergeFunctionsTest.cpp:function (anonymous namespace)::MergeFunctions_FalseOutputModuleTest_Test::TestBody(): error: undefined reference to 'llvm::MergeFunctionsPass::runOnModule(llvm::Module&)'
unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o:MergeFunctionsTest.cpp:function (anonymous namespace)::MergeFunctions_TrueOutputFunctionsTest_Test::TestBody(): error: undefined reference to 'llvm::MergeFunctionsPass::runOnFunctions(llvm::ArrayRef<llvm::Function*>)'
unittests/Transforms/Utils/CMakeFiles/UtilsTests.dir/MergeFunctionsTest.cpp.o:MergeFunctionsTest.cpp:function (anonymous namespace)::MergeFunctions_FalseOutputFunctionsTest_Test::TestBody(): error: undefined reference to 'llvm::MergeFunctionsPass::runOnFunctions(llvm::ArrayRef<llvm::Function*>)'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

@nikic
Copy link
Contributor

nikic commented Nov 28, 2024

Unit test moved in b96e757 to hopefully appease the buildbots.

@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 28, 2024

LLVM Buildbot has detected a new failure on builder libc-aarch64-ubuntu-fullbuild-dbg running on libc-aarch64-ubuntu while building llvm at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/71/builds/11524

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
[ RUN      ] LlvmLibcStrtouint64Test.MessyBaseSixteenDecode
[       OK ] LlvmLibcStrtouint64Test.MessyBaseSixteenDecode (2 us)
[ RUN      ] LlvmLibcStrtouint64Test.AutomaticBaseSelection
[       OK ] LlvmLibcStrtouint64Test.AutomaticBaseSelection (4 us)
Ran 14 tests.  PASS: 14  FAIL: 0
[883/884] Running unit test libc.test.src.time.nanosleep_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcNanosleep.SmokeTest
[       OK ] LlvmLibcNanosleep.SmokeTest (32 us)
Ran 1 tests.  PASS: 1  FAIL: 0
command timed out: 1200 seconds without output running [b'python', b'../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py', b'--debug'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=1244.063512
Step 8 (libc-unit-tests) failure: libc-unit-tests (failure)
...
[ RUN      ] LlvmLibcStrtouint32Test.DecodeInOtherBases
[       OK ] LlvmLibcStrtouint32Test.DecodeInOtherBases (314 ms)
[ RUN      ] LlvmLibcStrtouint32Test.CleanBaseSixteenDecode
[       OK ] LlvmLibcStrtouint32Test.CleanBaseSixteenDecode (8 us)
[ RUN      ] LlvmLibcStrtouint32Test.MessyBaseSixteenDecode
[       OK ] LlvmLibcStrtouint32Test.MessyBaseSixteenDecode (2 us)
[ RUN      ] LlvmLibcStrtouint32Test.AutomaticBaseSelection
[       OK ] LlvmLibcStrtouint32Test.AutomaticBaseSelection (4 us)
Ran 14 tests.  PASS: 14  FAIL: 0
[882/884] Running unit test libc.test.src.stdlib.strtoint64_test.__unit__
[==========] Running 14 tests from 1 test suite.
[ RUN      ] LlvmLibcStrtoint64Test.InvalidBase
[       OK ] LlvmLibcStrtoint64Test.InvalidBase (2 us)
[ RUN      ] LlvmLibcStrtoint64Test.CleanBaseTenDecode
[       OK ] LlvmLibcStrtoint64Test.CleanBaseTenDecode (14 us)
[ RUN      ] LlvmLibcStrtoint64Test.MessyBaseTenDecode
[       OK ] LlvmLibcStrtoint64Test.MessyBaseTenDecode (6 us)
[ RUN      ] LlvmLibcStrtoint64Test.DecodeInOtherBases
[       OK ] LlvmLibcStrtoint64Test.DecodeInOtherBases (312 ms)
[ RUN      ] LlvmLibcStrtoint64Test.CleanBaseSixteenDecode
[       OK ] LlvmLibcStrtoint64Test.CleanBaseSixteenDecode (8 us)
[ RUN      ] LlvmLibcStrtoint64Test.MessyBaseSixteenDecode
[       OK ] LlvmLibcStrtoint64Test.MessyBaseSixteenDecode (2 us)
[ RUN      ] LlvmLibcStrtoint64Test.AutomaticBaseSelection
[       OK ] LlvmLibcStrtoint64Test.AutomaticBaseSelection (3 us)
[ RUN      ] LlvmLibcStrtouint64Test.InvalidBase
[       OK ] LlvmLibcStrtouint64Test.InvalidBase (1 us)
[ RUN      ] LlvmLibcStrtouint64Test.CleanBaseTenDecode
[       OK ] LlvmLibcStrtouint64Test.CleanBaseTenDecode (6 us)
[ RUN      ] LlvmLibcStrtouint64Test.MessyBaseTenDecode
[       OK ] LlvmLibcStrtouint64Test.MessyBaseTenDecode (5 us)
[ RUN      ] LlvmLibcStrtouint64Test.DecodeInOtherBases
[       OK ] LlvmLibcStrtouint64Test.DecodeInOtherBases (309 ms)
[ RUN      ] LlvmLibcStrtouint64Test.CleanBaseSixteenDecode
[       OK ] LlvmLibcStrtouint64Test.CleanBaseSixteenDecode (8 us)
[ RUN      ] LlvmLibcStrtouint64Test.MessyBaseSixteenDecode
[       OK ] LlvmLibcStrtouint64Test.MessyBaseSixteenDecode (2 us)
[ RUN      ] LlvmLibcStrtouint64Test.AutomaticBaseSelection
[       OK ] LlvmLibcStrtouint64Test.AutomaticBaseSelection (4 us)
Ran 14 tests.  PASS: 14  FAIL: 0
[883/884] Running unit test libc.test.src.time.nanosleep_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcNanosleep.SmokeTest
[       OK ] LlvmLibcNanosleep.SmokeTest (32 us)
Ran 1 tests.  PASS: 1  FAIL: 0

command timed out: 1200 seconds without output running [b'python', b'../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py', b'--debug'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=1244.063512

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants